﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GadgetBase;

namespace Sidebar
{
    /// <summary>
    /// Interaction logic for AddGadgetWindow.xaml
    /// </summary>
    public partial class AddGadgetWindow : Window
    {
        private const string gadgetDirectory = @"Gadgets";

        public AddGadgetWindow()
        {
            InitializeComponent();            
        }

        public void RefreshGadgetList()
        {
            gadgetListBox.Items.Clear();

            GadgetWindow[] gadgets = ModuleManager.SearchGadgets(gadgetDirectory);            

            foreach (GadgetWindow gadget in gadgets)
            {
                AddItem(gadget);
            }
        }

        private void AddItem(GadgetWindow gadget)
        {
            Image image = new Image();
            image.Source = gadget.LogoImage;
            image.HorizontalAlignment = HorizontalAlignment.Center;
            image.VerticalAlignment = VerticalAlignment.Center;            
            Label label = new Label();
            label.Content = gadget.Title;
            label.HorizontalAlignment = HorizontalAlignment.Center;
            label.VerticalAlignment = VerticalAlignment.Bottom;

            Grid grid = new Grid();
            grid.VerticalAlignment = VerticalAlignment.Center;
            grid.HorizontalAlignment = HorizontalAlignment.Center;
            grid.Margin = new Thickness(6);
            grid.VerticalAlignment = VerticalAlignment.Stretch;
            grid.HorizontalAlignment = HorizontalAlignment.Stretch;
            
            RowDefinition topRow = new RowDefinition();                        
            topRow.Height = new GridLength(1, GridUnitType.Star);
            RowDefinition bottomRow = new RowDefinition();
            bottomRow.Height = new GridLength(25);
            grid.RowDefinitions.Add(topRow);
            grid.RowDefinitions.Add(bottomRow);

            grid.Children.Add(image);
            Grid.SetRow(image, 0);
            grid.Children.Add(label);
            Grid.SetRow(label, 1);
            grid.Tag = gadget;

            gadgetListBox.Items.Add(grid);
        }

        private object GetListBoxItemFromPoint(Point point)
        {
            UIElement element = gadgetListBox.InputHitTest(point) as UIElement;
            if (element != null)
            {
                //get the object from the element
                object data = DependencyProperty.UnsetValue;
                while (data == DependencyProperty.UnsetValue)
                {
                    // try to get the object value for the corresponding element
                    data = gadgetListBox.ItemContainerGenerator.ItemFromContainer(element);

                    //get the parent and we will iterate again
                    if (data == DependencyProperty.UnsetValue)
                        element = VisualTreeHelper.GetParent(element) as UIElement;

                    //if we reach the actual listbox then we must break to avoid an infinite loop
                    if (element == gadgetListBox)
                        return null;
                }

                //return the data that we fetched only if it is not Unset value, 
                //which would mean that we did not find the data
                if (data != DependencyProperty.UnsetValue)
                    return data;
            }

            return null;
        }

        private void dd(Image item)
        {
			// Create a brush which will paint the ListViewItem onto
			// a visual in the adorner layer.
            VisualBrush brush = new VisualBrush(item);

			// Create an element which displays the source item while it is dragged.
            DragAdorner dragAdorner = new DragAdorner(this.gadgetListBox, item.RenderSize, brush);
			
			// Set the drag adorner's opacity.		
            dragAdorner.Opacity = 200;

            AdornerLayer layer = AdornerLayer.GetAdornerLayer(this.gadgetListBox);
            layer.Add(dragAdorner);
        }

        private void gadgetListBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            object item = GetListBoxItemFromPoint(e.GetPosition(gadgetListBox));            

            if (item != null)
            {
                Grid grid = (Grid)item;
                GadgetWindow gadget = (GadgetWindow)grid.Tag;
                dd((Image)grid.Children[0]);
                //Shape shape = (Shape)grid;
                //DragDrop.DoDragDrop(gadgetListBox, shape, DragDropEffects.Scroll);
            }
        }
    }
}
